from django.utils.translation import gettext_lazy as _
from rest_framework import serializers


class AIHubODCSerializer(serializers.Serializer):
    id = serializers.CharField()
    name = serializers.CharField()
    name_en = serializers.CharField()
    sort_weight = serializers.IntegerField()
    organization = serializers.SerializerMethodField(method_name='get_organization')

    @staticmethod
    def get_organization(obj):
        organization = obj.organization
        if organization is None:
            return None

        return {'id': organization.id, 'name': organization.name, 'name_en': organization.name_en,
                'sort_weight': organization.sort_weight}


class AIHubServiceSerializer(serializers.Serializer):
    id = serializers.CharField()
    name = serializers.CharField()
    name_en = serializers.CharField()
    endpoint_url = serializers.CharField()
    creation = serializers.DateTimeField(label=_('创建时间'))
    modification = serializers.DateTimeField(label=_('修改时间'))
    status = serializers.CharField()
    longitude = serializers.FloatField(label=_('经度'))
    latitude = serializers.FloatField(label=_('纬度'))
    sort_weight = serializers.IntegerField(label=_('排序值'), help_text=_('值越小排序越靠前'))
    version = serializers.CharField(label=_('版本号'))
    version_update_time = serializers.DateTimeField(label=_('版本号更新时间'))
    org_data_center = AIHubODCSerializer(allow_null=True)


class AIHubServiceAdminSerializer(AIHubServiceSerializer):
    admin_users = serializers.SerializerMethodField(label=_('服务单元管理员'), method_name='get_admin_users')

    @staticmethod
    def get_admin_users(obj):
        return obj.admin_users
